home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994…tember: Reference Library / Dev.CD Sep 94.toast / Periodicals / develop / develop Issue 18 / develop 18 code / Hierarchical Lists / Src / WindowManager.c < prev   
Encoding:
Text File  |  1994-03-16  |  6.4 KB  |  234 lines  |  [TEXT/KAHL]

  1. /*
  2.  * These may be un-commented for testing.
  3.  */
  4. // #define FORCE_MONOCHROME        1
  5. // #define FORCE_RIGHT_JUSTIFY    1
  6. // #define FORCE_HUGE_FONT        1
  7. // #define NO_HILITE_SELECTION    1
  8. /*                                    WindowManager.c                                */
  9. /*
  10.  * List In A List Sample
  11.  * WindowManager.c
  12.  * Copyright © 1993-94 Apple Computer Inc.
  13.  *
  14.  * This is a very limited example of using the TwistDown list functions. In
  15.  * particular, note that there is no error handling whatsoever: if the disk
  16.  * being examined has too many files or folders, your program will crash.
  17.  */
  18. #include "ListInAList.h"
  19. #include <GestaltEqu.h>
  20. #include <Script.h>
  21. /*
  22.  * These configure the window for testing.
  23.  */
  24. #ifndef FORCE_MONOCHROME
  25. #define FORCE_MONOCHROME    0
  26. #endif
  27. #ifndef FORCE_RIGHT_JUSTIFY
  28. #define    FORCE_RIGHT_JUSTIFY    0
  29. #endif
  30. #ifndef FORCE_HUGE_FONT
  31. #define FORCE_HUGE_FONT        0
  32. #endif
  33. #ifndef NO_HILITE_SELECTION
  34. #define NO_HILITE_SELECTION    0
  35. #endif
  36.  
  37. /*
  38.  * MakeHFSBrowserWindow
  39.  * Create a window, the twist-down list, and fill it with file names.
  40.  */
  41. void
  42. MakeHFSBrowserWindow(void)
  43. {
  44.         register BrowserPtr                browserPtr;
  45.         WindowPtr                        theWindow;
  46.         Rect                            viewRect;
  47.         long                            response;
  48.         Boolean                            hasColorQuickDraw;
  49.         Ptr                                privateStash;
  50.         Str255                            work;
  51.         
  52.         hasColorQuickDraw = FALSE;
  53.         if (Gestalt(gestaltQuickdrawVersion, &response) == noErr
  54.          && response >= gestalt8BitQD)
  55.              hasColorQuickDraw = TRUE;
  56.         viewRect = qd.screenBits.bounds;
  57.         InsetRect(&viewRect, 4, 4);
  58.         viewRect.top += (GetMBarHeight() * 2);
  59.         browserPtr = (BrowserPtr) NewPtrClear(sizeof (BrowserRecord));
  60.         if (browserPtr == NULL)    
  61.             ExitToShell();
  62.         if (FORCE_MONOCHROME == 0 && hasColorQuickDraw) {
  63.             theWindow = NewCWindow(
  64.                         (Ptr) browserPtr,        /* Our data storage                */
  65.                         &viewRect,                /* Screen bounds                */
  66.                         "\pList in a Handle in a List",
  67.                         FALSE,                    /* Initially invisible            */
  68.                         noGrowDocProc,            /* Simple document                */
  69.                         (WindowPtr) -1L,        /* Put in front                    */
  70.                         TRUE,                    /* Has go away                    */
  71.                         0                        /* No refCon                    */
  72.                     );
  73.         }
  74.         else {
  75.             theWindow = NewWindow(
  76.                         (Ptr) browserPtr,        /* Our data storage                */
  77.                         &viewRect,                /* Screen bounds                */
  78.                         "\pList in a Handle in a List",
  79.                         FALSE,                    /* Initially invisible            */
  80.                         noGrowDocProc,            /* Simple document                */
  81.                         (WindowPtr) -1L,        /* Put in front                    */
  82.                         TRUE,                    /* Has go away                    */
  83.                         0                        /* No refCon                    */
  84.                     );
  85.         }
  86.         if (theWindow == NULL)
  87.             ExitToShell();
  88.         SetPort(theWindow);                        /* Do this first, or else        */
  89.         viewRect = theWindow->portRect;
  90.         TextFont(kListFontNumber);
  91. #if FORCE_HUGE_FONT
  92.         /*
  93.          * This makes the font size huge so the buttons can easily be placed
  94.          * across multiple monitors in order to test the DeviceLoop algorithm.
  95.          */
  96.         TextSize((viewRect.bottom - viewRect.top) / 8);        /* A huge font size    */
  97. #else
  98.         TextSize(kListFontSize);
  99. #endif
  100.         browserPtr->theList = NewTwistDownList(
  101.                     &viewRect,                    /* Where it goes in the window    */
  102.                     NULL,                        /* Draw using DrawText            */
  103.                     CharWidth('n'),                /* Tab indentation amount        */
  104.                     NO_HILITE_SELECTION,        /* Hilite selections?            */
  105. #if FORCE_RIGHT_JUSTIFY
  106.                     FALSE
  107. #else
  108.                     (GetSysJust() == 0)            /* Current system justification    */
  109. #endif
  110.                 );
  111.         if (browserPtr->theList == NULL)
  112.             ExitToShell();                        /* Oops                            */
  113.         ShowWindow(theWindow);
  114.         SelectWindow(theWindow);
  115.         gUpdateMenusNeeded = TRUE;
  116.         ++gOpenWindowCount;
  117.         SetCursor(*GetCursor(watchCursor));
  118.         /*
  119.          * Because EnumerateHFSCatalog can absorb all available memory if you
  120.          * call it for a very large disk (ask me how I know), we grab a chunk
  121.          * of memory that we restore before building and displaying the visible.
  122.          * This isn't a really great solution, but it will do for now.
  123.          */
  124.         privateStash = NewPtr(32768L);
  125.         if (privateStash != NULL) {
  126.             browserPtr->listHead = EnumerateHFSCatalog(
  127.                 browserPtr->theList,            /* Store in this list            */
  128.                 0,                                /* Initial indent                */
  129.                 0,                                /* Do current volume            */
  130.                 fsRtDirID                        /* Start at the root directory    */
  131.             );
  132.             DisposePtr(privateStash);
  133.         }
  134.         /*
  135.          * The catalog has been enumerated and the list of files and folders
  136.          * constructed. Display the number of files/folders in the Window title
  137.          * and display the initial list.
  138.          */
  139.         NumToString(CountListElements(browserPtr->listHead), work);
  140.         pstrcat(work, "\p - Files and folders found");
  141.         SetWTitle(theWindow, work);
  142.         CreateVisibleList(browserPtr->theList, browserPtr->listHead);
  143. }
  144.  
  145. /*
  146.  * DisposeBrowser
  147.  * The user clicked on the closer. Dispose of the window and its contents.
  148.  */
  149. void
  150. DisposeBrowser(
  151.         register BrowserPtr                browserPtr
  152.     )
  153. {
  154.         DisposeTwistDownList(browserPtr->theList);
  155.         DisposeTwistDownHdl(browserPtr->listHead, NULL, 0);
  156.         CloseWindow((WindowPtr) &browserPtr->theWindow);
  157.         DisposePtr((Ptr) browserPtr);
  158.         gUpdateMenusNeeded = TRUE;
  159.         --gOpenWindowCount;
  160. }
  161.  
  162. /*
  163.  * ActivateBrowser
  164.  * This is called on activate and deactivate (or suspend/resume) events to enable
  165.  * or disable the window content.
  166.  */
  167. void
  168. ActivateBrowser(
  169.         register BrowserPtr                browserPtr,
  170.         Boolean                            activating
  171.     )
  172. {
  173.         LActivate(activating, browserPtr->theList);
  174. }
  175.  
  176. /*
  177.  * DoWindowKeyDown
  178.  */
  179. void
  180. DoWindowKeyDown(
  181.         register BrowserPtr                browserPtr
  182.     )
  183. {
  184. #pragma unused (browserPtr)
  185.  
  186.         /* Nothing happens here */
  187. }
  188.  
  189. /*
  190.  * DoContentClick
  191.  * Click in the window. This function decides which element received the click and
  192.  * calls the requisite click handler. In this example, we need only deal with
  193.  * the twist-down list.
  194.  */
  195. void
  196. DoContentClick(
  197.         register BrowserPtr                browserPtr
  198.     )
  199. {
  200.         Cell                            theCell;
  201.         TwistDownClickState                clickState;
  202.         
  203.         clickState = DoTwistDownClick(browserPtr->theList, &gEventRecord, &theCell);
  204.         switch (clickState) {
  205.         case kTwistDownNotInList:            /* Not in list view rectangle        */
  206.         case kTwistDownNoClick:                /* Scroll bar or user changed mind    */
  207.             break;                            /* Ignore this click                */
  208.         case kTwistDownButtonClick:            /* Clicked on expansion button        */
  209.             break;                            /* Done for us                        */
  210.         case kTwistDownClick:                /* Single-click on list datum        */
  211.         case kTwistDownDoubleClick:            /* Double-click on list datum        */
  212.             break;                            /* Ignore these clicks                */
  213.         }
  214. }
  215.  
  216. /*
  217.  * UpdateBrowserWinow
  218.  * Process an update event in this window.
  219.  */
  220. void
  221. UpdateBrowserWindow(
  222.         register BrowserPtr                browserPtr,
  223.         RgnHandle                        updateRgn
  224.     )
  225. {
  226.         Rect                            viewRect;
  227.         
  228.         viewRect = (**(browserPtr->theList)).rView;
  229.         InsetRect(&viewRect, -1, -1);
  230.         FrameRect(&viewRect);
  231.         LUpdate(updateRgn, browserPtr->theList);
  232. }
  233.  
  234.